/**
 * Created by xiaolinzhang on 2017/7/29.
 */
var mysqlConn = require("./BaseDao");

exports.getCommInfo = function (userId,callbk) {
    mysqlConn.getConn(function (err,connection) {
        connection.query("select a.*,DATE_FORMAT(a.create_time,'%Y-%m-%d %H:%i:%s') as comm_time,(b.prod_sum*prod_price) as pay_price from commission a left join wh_order b on a.ord_id = b.ord_id where a.user_id = ? order by a.create_time desc limit 20",[userId],function (err,rows) {
            connection.release();
            console.log(err);
            callbk(err,rows);
        })
    });
}

exports.getCommSum = function (userId,callbk) {
    mysqlConn.getConn(function (err,connection) {
        connection.query("select sum(comm_value) as comm from commission where user_id = ?",[userId],function (err,rows) {
            connection.release();
            console.log(err);
            callbk(err,rows);
        })
    });
}

exports.getCommSumByState = function (userId,callbk) {
    mysqlConn.getConn(function (err,connection) {
        connection.query("select IFNULL(sum(comm_value),0) as comm from commission where user_id = ? and state = 0",[userId],function (err,rows) {
            connection.release();
            console.log(err);
            callbk(err,rows);
        })
    });
}

exports.getComm = function (userId,callbk) {
    mysqlConn.getConn(function (err,connection) {
        connection.query("select comm_value,comm_id from commission where user_id = ? and state = 0",[userId],function (err,rows) {
            connection.release();
            console.log(err);
            callbk(err,rows);
        })
    });
}

exports.getCommDetail = function (userId,sign,callbk) {
    var sql = "select a.*,DATE_FORMAT(a.create_time,'%Y-%m-%d %H:%i:%s') as comm_time,(b.prod_sum*prod_price) as pay_price from commission a left join wh_order b on a.ord_id = b.ord_id where a.user_id = ? ";
    if(sign == 1){
        sql += " and month(a.create_time) = month(now())";
    }else if(sign == 2){
        sql += "and year(a.create_time) = year(now())";
    }
    sql += " order by a.create_time desc";
    mysqlConn.getConn(function (err,connection) {
        connection.query(sql,[userId],function (err,rows) {
            connection.release();
            console.log(err);
            callbk(err,rows);
        })
    });
};

exports.getDeposList = function (userId,callbk) {
    var sql = "select DATE_FORMAT(create_time,'%Y-%m-%d %H:%i:%s') as ct_time,comm_total,(case state when 1 then '审核中' when 2 then '成功' when 3 then '失败' else '未处理' end) as state,pay_method from depos_info where user_id = ?";
    mysqlConn.getConn(function (err,connection) {
        connection.query(sql,[userId],function (err,rows) {
            connection.release();
            console.log(err);
            callbk(err,rows);
        })
    });
}

exports.insDeposInfo = function (userId,commTotal,state,commIds,payMethod,phone,callbk) {
    var sql = "insert into depos_info(user_id,comm_total,state,comm_ids,pay_method,phone) values(?,?,?,?,?,?)";
    mysqlConn.getConn(function (err,connection) {
        connection.query(sql,[userId,commTotal,state,commIds,payMethod,phone],function (err,result) {
            connection.release();
            console.log(err);
            callbk(err,result);
        })
    });
}


exports.updCommState = function (commIds,callbk) {
    var sql = "update commission set state = 1 where comm_id in ("+commIds+")";
    mysqlConn.getConn(function (err,connection) {
        connection.query(sql,[],function (err,result) {
            connection.release();
            console.log(err);
            callbk(err,result);
        })
    });
}